home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 03 Alexander / LoadBalance.h < prev   
Encoding:
C/C++ Source or Header  |  2001-08-18  |  9.6 KB  |  384 lines

  1. /*********************************************************************\
  2.  *
  3.  * Workfile: LoadBalance.h
  4.  *
  5.  * Author: Balexander
  6.  *
  7.  *
  8.  * Description:
  9.  *
  10.  *   This header file describes the AI update module class.
  11.  *
  12.  *
  13. \*********************************************************************/
  14.  
  15.  
  16. #ifndef __loadbalance_h__
  17. #define __loadbalance_h__
  18.  
  19.  
  20. class Task;
  21. class SchedulingGroup;
  22.  
  23.  
  24. ///////////////////////////////////////////////////////////////////////
  25. // AI Update Module Profiling
  26. ///////////////////////////////////////////////////////////////////////
  27.  
  28. #if _DEBUG
  29.  
  30. struct ProfileData 
  31. {
  32.     float                    TotalTime;
  33.     float                    MinTime;
  34.     float                    MaxTime;
  35.     unsigned long            Count;
  36.     const char *            Name;
  37. };
  38.  
  39. struct GroupProfileData 
  40. {
  41.     float                    TotalTime;
  42.     float                    MaxTime;
  43.     unsigned long            FrameCount;
  44.     unsigned long            FrameTaskCount;
  45.     unsigned long            LastFrame;
  46.     float                    LastFrameTotalTime;
  47. };
  48.  
  49. #define    DECLARE_AI_UPDATE_PROFILE_ACCESS    ProfileData &    GetProfileData( void )    { return mProfileData; }
  50. #define    DECLARE_AI_UPDATE_PROFILE_DATA        ProfileData        mProfileData;
  51.  
  52. #define    DECLARE_AI_GROUP_UPDATE_PROFILE_ACCESS    GroupProfileData &    GetGroupProfileData( void )    { return mGroupProfileData; }
  53. #define    DECLARE_AI_GROUP_UPDATE_PROFILE_DATA    GroupProfileData    mGroupProfileData;
  54.  
  55. #define AI_UPDATE_SET_PROFILE_NAME( object, name )    ((object).GetProfileData().Name = (name))
  56.  
  57. #else    //    _DEBUG
  58.  
  59. #define    DECLARE_AI_UPDATE_PROFILE_ACCESS
  60. #define    DECLARE_AI_UPDATE_PROFILE_DATA
  61.  
  62. #define    DECLARE_AI_GROUP_UPDATE_PROFILE_ACCESS
  63. #define    DECLARE_AI_GROUP_UPDATE_PROFILE_DATA
  64.  
  65. #define AI_UPDATE_SET_PROFILE_NAME( object, name )    ((void)0)
  66.  
  67. #endif    //    _DEBUG
  68.  
  69. ///////////////////////////////////////////////////////////////////////
  70. // Execution module class
  71. ///////////////////////////////////////////////////////////////////////
  72.  
  73. class ExecutionModule
  74. {
  75. public:
  76.  
  77.     // initialization/release
  78.     bool Init( void );
  79.     bool Release( void );
  80.  
  81.     void Update( unsigned long dt );
  82.  
  83.     float            GetTime( void )        { return mTime; }
  84.     unsigned long    GetTicks( void )    { return mTicks; }
  85.     unsigned long    GetFrames( void )    { return mFrames; }
  86.  
  87.     bool    AttachGroup( SchedulingGroup * group );
  88.     bool    DetachGroup( SchedulingGroup * group );
  89.  
  90.     bool    ScheduleTask( Task * task );
  91.     bool    UnscheduleTask( Task * task );
  92.  
  93.     Task *    GetFirstScheduledTask( void )    { return mpScheduledTasks; }
  94.  
  95.     SchedulingGroup *    GetFirstGroup( void )    { return mpFirstGroup; }
  96.  
  97.     DECLARE_AI_UPDATE_PROFILE_ACCESS
  98.  
  99. private:
  100.  
  101.     float                        mTime;            // Current game time in seconds
  102.     unsigned long                mTicks;            // Current game time in ms.
  103.     unsigned long                mFrames;        // Current game frame count
  104.  
  105.     Task *                        mpScheduledTasks;
  106.  
  107.     Task *                        mpExecutingTask;
  108.  
  109.     SchedulingGroup *            mpFirstGroup;
  110.  
  111.     DECLARE_AI_UPDATE_PROFILE_DATA
  112. };
  113.  
  114. extern ExecutionModule    gAIUpdateModule;
  115.  
  116. ///////////////////////////////////////////////////////////////////////
  117. // Scheduling Group Class
  118.  
  119. class SchedulingGroup
  120. {
  121.     friend bool ExecutionModule::AttachGroup( SchedulingGroup * group );
  122.     friend bool ExecutionModule::DetachGroup( SchedulingGroup * group );
  123.  
  124. public:
  125.  
  126.     // initialization/release
  127.     bool Init( void );
  128.     bool Release( void );
  129.  
  130.     virtual bool        AttachTask( Task * task );
  131.     virtual bool        DetachTask( Task * task );
  132.  
  133.     void                SetTaskTime( Task * task, float time );
  134.  
  135.     virtual void        Schedule( ExecutionModule * module ) = 0;
  136.  
  137.     Task *                GetFirstTask( void )    { return mpFirstTask; }
  138.     SchedulingGroup *    GetNext( void )            { return mpNext; }
  139.  
  140.     int                    GetTaskCount( void )    { return mTaskCount; }
  141.  
  142.     DECLARE_AI_UPDATE_PROFILE_ACCESS
  143.     DECLARE_AI_GROUP_UPDATE_PROFILE_ACCESS
  144.  
  145. protected:
  146.  
  147.     Task *                mpFirstTask;
  148.     int                    mTaskCount;
  149.  
  150.     SchedulingGroup *    mpNext;
  151.  
  152.     DECLARE_AI_UPDATE_PROFILE_DATA
  153.     DECLARE_AI_GROUP_UPDATE_PROFILE_DATA
  154. };
  155.  
  156. ///////////////////////////////////////////////////////////////////////
  157. // Task Class
  158.  
  159. class Task
  160. {
  161.     friend bool SchedulingGroup::AttachTask( Task * task );
  162.     friend bool SchedulingGroup::DetachTask( Task * task );
  163.     friend void SchedulingGroup::SetTaskTime( Task * task, float time );
  164.     friend bool ExecutionModule::UnscheduleTask( Task * task );
  165.     friend bool ExecutionModule::ScheduleTask( Task * task );
  166.     friend void ExecutionModule::Update( unsigned long dt );
  167.  
  168. public:
  169.     ~Task()    { Release(); }    // Just in case
  170.  
  171.     // initialization/release
  172.     bool Init( void );
  173.     bool Release( void );
  174.  
  175.     // This function gets called to do the actual work.
  176.     virtual void Run( float dt ) = 0;
  177.  
  178.     bool    IsScheduled( void )        { return mScheduled; }
  179.  
  180.     float GetTime( void)        { return mTime; }
  181.     float GetLastTime( void)    { return mLastTime; }
  182.  
  183.     SchedulingGroup *        GetGroup( void )        { return mpGroup; }
  184.  
  185.     Task *    GetGroupNext( void )    { return mpGroupNext; }
  186.     Task *    GetScheduleNext( void )    { return mpScheduledNext; }
  187.     
  188.     DECLARE_AI_UPDATE_PROFILE_ACCESS
  189.  
  190. protected:
  191.  
  192.     float                    mTime;            // Current schedule time in game seconds
  193.     float                    mLastTime;        // Last time the task was fired.
  194.     bool                    mScheduled;        // True if currently scheduled, o/w false
  195.  
  196.     SchedulingGroup *        mpGroup;
  197.  
  198.     Task *                    mpGroupNext;
  199.     Task *                    mpScheduledNext;
  200.  
  201.     DECLARE_AI_UPDATE_PROFILE_DATA
  202. };
  203.  
  204. ///////////////////////////////////////////////////////////////////////
  205. // Standard Scheduler Tasks
  206. ///////////////////////////////////////////////////////////////////////
  207.  
  208. ///////////////////////////////////////////////////////////////////////
  209. // AI Timed Task
  210.  
  211. class TimedTask : public Task
  212. {
  213. public:
  214.  
  215.     // initialization/release
  216.     bool Init( void );
  217.  
  218.     // Reset all stats.
  219.     void Reset( void );
  220.  
  221.     // This function will time the run of the Execute function below.
  222.     virtual void Run( float dt );
  223.  
  224.     virtual void Execute( float dt ) = 0;
  225.  
  226.     float            GetLastExecutionTime( void )    { return mLastTime; }
  227.     float            GetTotalExecutionTime( void )    { return mTotalTime; }
  228.     unsigned long    GetExecutionCount( void )        { return mExecutionCount; }
  229.  
  230. protected:
  231.  
  232.     float            mLastTime;            // Last execution duration in seconds.
  233.     float            mTotalTime;            // Total execution time in seconds for the current game.
  234.     unsigned long    mExecutionCount;    // Number of times the function has executed in the current game.
  235.  
  236. };
  237.  
  238.  
  239. ///////////////////////////////////////////////////////////////////////
  240. // AI Variable Delay Task
  241.  
  242. class VarDelayTask : public Task
  243. {
  244. public:
  245.  
  246.     // initialization/release
  247.     bool Init( void );
  248.  
  249.     void    SetDelay( float seconds )    { mDelay = seconds; }
  250.     float    GetDelay( void )            { return mDelay; }
  251.  
  252. protected:
  253.  
  254.     float        mDelay;        // Delay in seconds
  255.  
  256. };
  257.  
  258.  
  259. ///////////////////////////////////////////////////////////////////////
  260. // Standard Scheduler Groups
  261. ///////////////////////////////////////////////////////////////////////
  262.  
  263. ///////////////////////////////////////////////////////////////////////
  264. // AI Update Every Frame Group
  265.  
  266. class EveryFrameGroup : public SchedulingGroup
  267. {
  268. public:
  269.     virtual void Schedule( ExecutionModule * module );
  270. };
  271.  
  272.  
  273. ///////////////////////////////////////////////////////////////////////
  274. // AI Count Update Group
  275.  
  276. class CountGroup : public SchedulingGroup
  277. {
  278. public:
  279.  
  280.     // initialization/release
  281.     bool Init( int count = 1 );
  282.     bool Release( void );
  283.  
  284.     virtual bool    DetachTask( Task * task );
  285.  
  286.     void    SetCount( int count )        { mCount = count; }
  287.     int        GetCount( void )            { return mCount; }
  288.  
  289.     virtual void Schedule( ExecutionModule * module );
  290.  
  291. protected:
  292.  
  293.     int        mCount;        // Number of allowed executions per frame.
  294.     Task *    mpCurrentTask;
  295.  
  296. };
  297.  
  298. ///////////////////////////////////////////////////////////////////////
  299. // AI Spread Update Group
  300.  
  301. class SpreadGroup : public SchedulingGroup
  302. {
  303. public:
  304.  
  305.     // initialization/release
  306.     bool Init( float period = 1.0f/4.0f );
  307.  
  308.     virtual bool    AttachTask( Task * task );
  309.     virtual bool    DetachTask( Task * task );
  310.  
  311.     void    SetPeriod( float period )    { mPeriod = period; }
  312.     float    GetPeriod( void )            { return mPeriod; }
  313.  
  314.     virtual void Schedule( ExecutionModule * module );
  315.  
  316. protected:
  317.  
  318.     float        mTime;        // Current next scheduling time in seconds.
  319.     float        mPeriod;    // Delay period (in seconds) between same task executions.
  320. };
  321.  
  322. ///////////////////////////////////////////////////////////////////////
  323. // AI Max Time Update Group
  324.  
  325. class MaxTimeGroup : public SchedulingGroup
  326. {
  327. public:
  328.  
  329.     // initialization/release
  330.     bool Init( float MaxTime );
  331.     bool Release( void );
  332.  
  333.     // These make sure that only TimedTask's can be
  334.     // attached to this group.
  335.     bool    AttachTask( TimedTask * task );
  336.     bool    DetachTask( TimedTask * task );
  337.  
  338.     void    SetMaxTime( float MaxTime )    { mMaxTime = MaxTime; }
  339.     float    GetMaxTime( float MaxTime )    { return mMaxTime; }
  340.  
  341.     virtual void Schedule( ExecutionModule * module );
  342.  
  343. protected:
  344.  
  345.     // By making these protected, we make sure that only 
  346.     // TimedTask's can be attached to this group.
  347.     virtual bool    AttachTask( Task * task );
  348.     virtual bool    DetachTask( Task * task );
  349.  
  350.     float    mMaxTime;        // Max group execution time (in seconds) per frame.
  351.     Task *    mpCurrentTask;
  352. };
  353.  
  354.  
  355. ///////////////////////////////////////////////////////////////////////
  356. // AI Variable Delay Update Group
  357.  
  358. class VarDelayGroup : public SchedulingGroup
  359. {
  360. public:
  361.  
  362.     // initialization/release
  363.     bool Init( void );
  364.  
  365.     // These make sure that only TimedTask's can be
  366.     // attached to this group.
  367.     bool    AttachTask( VarDelayTask * task );
  368.     bool    DetachTask( VarDelayTask * task );
  369.  
  370.     virtual void Schedule( ExecutionModule * module );
  371.  
  372. protected:
  373.  
  374.     // By making these protected, we make sure that only 
  375.     // TimedTask's can be attached to this group.
  376.     virtual bool    AttachTask( Task * task );
  377.     virtual bool    DetachTask( Task * task );
  378.  
  379.     float            mTime;        // Scheduling time for new tasks in seconds.
  380. };
  381.  
  382.  
  383. #endif __loadbalance_h__
  384.